<html>
<head><meta charset="utf-8"><title>Return a borrow from a closure? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html">Return a borrow from a closure?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229338360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229338360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229338360">(Mar 08 2021 at 17:17)</a>:</h4>
<p>Is there a way to have a <code>Fn(&amp;str) -&gt; &amp;str</code> closure? the compiler seems to not be applying elision rules:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span>: <span class="nc">lifetime</span><span class="w"> </span><span class="n">may</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">live</span><span class="w"> </span><span class="n">long</span><span class="w"> </span><span class="n">enough</span><span class="w"></span>
<span class="w">   </span><span class="o">-</span>-&gt; <span class="nc">crates</span><span class="o">/</span><span class="n">rust</span><span class="o">-</span><span class="n">analyzer</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">config</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">793</span>:<span class="mi">13</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"></span>
<span class="mi">789</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="n">doc_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">line</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="o">|</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">str</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w">                           </span><span class="o">-</span><span class="w">        </span><span class="o">-</span><span class="w"> </span><span class="kd">let</span><span class="o">'</span><span class="na">s</span><span class="w"> </span><span class="n">call</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">lifetime</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">reference</span><span class="w"> </span><span class="err">`</span><span class="o">'</span><span class="mi">2</span><span class="err">`</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w">                           </span><span class="o">|</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w">                           </span><span class="kd">let</span><span class="o">'</span><span class="na">s</span><span class="w"> </span><span class="n">call</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">lifetime</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">reference</span><span class="w"> </span><span class="err">`</span><span class="o">'</span><span class="mi">1</span><span class="err">`</span><span class="w"></span>
<span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="mi">793</span><span class="w"> </span><span class="o">|</span><span class="w">             </span><span class="n">line</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w">             </span><span class="o">^^^^</span><span class="w"> </span><span class="n">returning</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">requires</span><span class="w"> </span><span class="n">that</span><span class="w"> </span><span class="err">`</span><span class="o">'</span><span class="mi">1</span><span class="err">`</span><span class="w"> </span><span class="n">must</span><span class="w"> </span><span class="n">outlive</span><span class="w"> </span><span class="err">`</span><span class="o">'</span><span class="mi">2</span><span class="err">`</span><span class="w"></span>
</code></pre></div>



<a name="229338476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229338476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229338476">(Mar 08 2021 at 17:18)</a>:</h4>
<p>I can turn it into a function without changing the body and that fixes it, but means I can't capture local variables</p>



<a name="229338637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229338637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229338637">(Mar 08 2021 at 17:19)</a>:</h4>
<p>lol what a hack, this fixed it</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">generate_doc</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">doc</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="p">[</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">],</span><span class="w"> </span><span class="n">mode</span>: <span class="nc">DocMode</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">doc_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">line</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="kt">str</span><span class="o">|</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="kt">str</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="fm">matches!</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span><span class="w"> </span><span class="n">DocMode</span>::<span class="n">Json</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">line</span><span class="p">.</span><span class="n">trim</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">line</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>i.e. putting the lifetime on the enclosing function, not the closure</p>



<a name="229338820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229338820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229338820">(Mar 08 2021 at 17:20)</a>:</h4>
<p>Maybe <a href="https://github.com/rust-lang/rust/issues/22340">this</a> is related?</p>



<a name="229338922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229338922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229338922">(Mar 08 2021 at 17:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="374396">hyd-dev</span> <a href="#narrow/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F/near/229338820">said</a>:</p>
<blockquote>
<p>Maybe <a href="https://github.com/rust-lang/rust/issues/22340">this</a> is related?</p>
</blockquote>
<p>related, but not the same - that wants a way to declare explicit lifetimes on the closure, I just want normal elision rules to be applied</p>



<a name="229339058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229339058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229339058">(Mar 08 2021 at 17:21)</a>:</h4>
<p>I think this is <a href="https://github.com/rust-lang/rust/issues/56537">https://github.com/rust-lang/rust/issues/56537</a>, which was closed as a breaking change :(</p>



<a name="229590629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229590629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229590629">(Mar 10 2021 at 02:12)</a>:</h4>
<p>FWIW the general workaround is what I've decided to dub "closure funnelling":</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">higher_order_funnel</span><span class="o">&lt;</span><span class="n">F</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">f</span>: <span class="nc">F</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">F</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">F</span><span class="w"> </span>: <span class="nb">FnOnce</span><span class="p">(</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="c1">// &lt;- here elision kicks in correctly</span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">doc_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">higher_order_funnel</span><span class="p">(</span><span class="o">|</span><span class="n">line</span><span class="o">|</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="err">…</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">line</span><span class="p">.</span><span class="n">trim</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">line</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
</code></pre></div>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> note that the <code>generate_docs</code> example there will be yielding an <code>impl Fn(&amp;'a str) -&gt; &amp;'a str</code> closure, instead of the <code>impl for&lt;'any&gt; Fn(&amp;'any str) -&gt; &amp;'any str</code> you asked for in the OP <span aria-label="warning" class="emoji emoji-26a0" role="img" title="warning">:warning:</span> (but I guess that signature sufficed for your use-case)</p>



<a name="229590785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229590785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229590785">(Mar 10 2021 at 02:14)</a>:</h4>
<p>Wait, what's the difference? I thought the first one has to work for arbitrary 'a lifetimes?</p>



<a name="229591902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229591902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229591902">(Mar 10 2021 at 02:27)</a>:</h4>
<p>It depends on which "level" you reason. To see what I mean, create (within the body of <code>generate_doc</code>) a <code>let local: &amp;str = &amp;String::from("local");</code>, and try to call <code>doc_line</code> on it. A true <code>for&lt;'any&gt; &amp;'any str -&gt; &amp;'any</code> would be able to handle that, but yours won't, since that "<code>'local</code>" lifetime won't match the function-given <code>'a</code> ((non-higher-order) generic parameters are necessarily bigger than local-to-function-body lifetimes).<br>
But if by "arbitrary", you mean any string that the caller of <code>generate_doc</code> may give, then yes, <code>doc_line</code> will work just fine <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="229598406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Return%20a%20borrow%20from%20a%20closure%3F/near/229598406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Return.20a.20borrow.20from.20a.20closure.3F.html#229598406">(Mar 10 2021 at 03:46)</a>:</h4>
<p>ah ok - <code>'a</code> already bound by the function, so it's not arbitrary from <code>doc_line</code>'s perspective, just from <code>generate_doc</code>'s</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>